using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GiftNet.IDAL;
using GiftNet.DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace GiftNet.SQLServerDAL
{
    #region dbo.producttags DAL -(by fangyifeng at 2010-12-05 14:12:11)
    public class Producttag : IProducttag
    {

        /// <summary>
        /// Check exists for PrimaryKey
        /// </summary>
        public bool Exists(Guid TagId)
        {
            try
            {
                NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
                GiftNet.Model.Producttag model = (GiftNet.Model.Producttag)session.Load(typeof(GiftNet.Model.Producttag), TagId);
                session.Close();

                if (model != null)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Add record for model
        /// </summary>
        public bool Add(GiftNet.Model.Producttag producttagsModel)
        {
            bool ret = true;
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            try
            {
                session.Save(producttagsModel);
                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                ret = false;
            }
            finally
            {
                session.Close();
            }

            return ret;
        }

        /// <summary>
        /// Add record for model and return the key object 
        /// </summary>
        public object AddWithReturnKey(GiftNet.Model.Producttag producttagsModel)
        {
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            object obj = null;
            try
            {
                obj = session.Save(producttagsModel);
                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                obj = null;
            }
            finally
            {
                session.Close();
            }

            return obj;
        }

        /// <summary>
        /// Update record for model		
        /// </summary>
        public bool Update(GiftNet.Model.Producttag producttagsmodel)
        {
            bool ret = true;
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();
            try
            {
                session.Update(producttagsmodel);

                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
                session.Close();
            }
            catch
            {
                transaction.Rollback();
                ret = false;
            }
            finally
            {
                transaction.Dispose();
                session.Close();
            }

            return ret;
        }


        /// <summary>
        /// Delete record
        /// </summary>
        public bool Delete(GiftNet.Model.Producttag producttagsModel)
        {
            try
            {
                NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
                NHibernate.ITransaction transaction = session.BeginTransaction();

                session.Delete(producttagsModel);

                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
                session.Close();

                return true;
            }
            catch
            {
                return false;
            }
        }



        /// <summary>
        /// Delete record for PrimaryKey
        /// </summary>
        public bool Delete(Guid TagId)
        {
            bool ret = true;
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            try
            {
                // Tell NHibernate that this object should be saved
                GiftNet.Model.Producttag model = (GiftNet.Model.Producttag)session.Load(typeof(GiftNet.Model.Producttag), TagId);
                // 
                session.Delete(model);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                transaction.Dispose();
                session.Close();

                ret = false;
            }

            return ret;
        }


        /// <summary>
        /// Get Model fro PrimaryKey
        /// </summary>
        public GiftNet.Model.Producttag GetModel(Guid TagId)
        {
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            GiftNet.Model.Producttag model = (GiftNet.Model.Producttag)session.Load(typeof(GiftNet.Model.Producttag), TagId);

            transaction.Commit();
            session.Close();

            return model;
        }


        /// <summary>
        /// Get ModelList fro Where no suppose Page
        /// </summary>
        /// <param name="iNum">return the record's num,IF not need please insert ""</param>
        /// <param name="strWhere">strWhere,need insert KeyWord->WHERE IF you need this Param</param>
        /// <param name="strOrderBy">strOrderBy,need insert KeyWord->Order By IF you need this Param</param>
        /// <returns>IList&lt;GiftNet.Model.Producttag&gt;</returns>
        public IList<GiftNet.Model.Producttag> GetModelList(string strWhere)
        {
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            return session.CreateQuery(strWhere).List<GiftNet.Model.Producttag>();
        }


        /// <summary>
        /// Get ModelList fro Where suppose Page
        /// </summary>
        /// <param name="PageSize">PageSize</param>
        /// <param name="PageIndex">PageIndex</param>
        /// <param name="recordCount">recordCount</param>
        /// <param name="strWhere">strWhere</param>
        /// <returns>IList&lt;GiftNet.Model.Producttag&gt;</returns>
        public IList<GiftNet.Model.Producttag> GetModelList(int PageSize, int PageIndex, out int recordCount, string strWhere)
        {

            recordCount = 0;
            IList sList = GiftNet.DBUtility.DBHelper.Query(" select producttag from Producttag producttag " + ((strWhere.Trim().Length <= 0) ? "" : " where " + strWhere), PageIndex, PageSize);

            if (sList.Count <= 0)
                return null;
            IList<GiftNet.Model.Producttag> sRetList = new List<GiftNet.Model.Producttag>();
            foreach (object obj in sList)
            {
                sRetList.Add((GiftNet.Model.Producttag)obj);
            }
            recordCount = GiftNet.DBUtility.DBHelper.Query("select count(1) from Producttag producttag " + ((strWhere.Trim().Length <= 0) ? "" : " where " + strWhere)).Count;

            return sRetList;
        }
    }
    #endregion
}

